<template>
  <div class="card-container">
    <div
      v-for="category in filteredCategories"
      :key="category.id"
      class="card"
      @click="selectCategory(category)"
    >
      <img
        :alt="category.name"
        :src="category.cover || `https://picsum.photos/240/125?random=${category.id}`"
        class="card-img"
      />
      <div class="card-body">
        <h6 class="card-title">{{ category.name }}</h6>
        <p class="card-text">文章数量：{{ category.article_count }}篇</p>
        <p class="card-description">{{ category.description }}</p>
      </div>
    </div>
    <div v-if="!filteredCategories.length" class="no-data">
      <p>暂无文章分类数据</p>
      </div>
  </div>
</template>

<script setup>
const router = useRouter()
const categories = ref([]);
const emit = defineEmits(['select-category']);

// 计算属性：过滤有文章的分类
const filteredCategories = computed(() =>
  categories.value.filter(category => category.article_count > 0)
);

// 获取分类数据
const fetchCategories = async () => {
  try {
    const response = await axios.get('/api/cms/categories/get_category_article_info/');
    if (response.data && Array.isArray(response.data)) {
      categories.value = response.data;
    } else {
      console.error('分类数据格式不正确:', response.data);
    }
  } catch (error) {
    console.error('获取分类数据失败:', error);
  }
};

// 分类选择处理
const selectCategory = (category) => {

  router.push({
    name: 'subject-detail',  // 使用你路由中定义的name
    params: {
      id: category.id,
    }
  })
  emit('select-category', category)
};



// 生命周期钩子
onMounted(() => {
  fetchCategories();
});
</script>

<style scoped>
/* 原有样式保持不变 */
.card-container {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  gap: 20px;
  margin-top: 20px;
}

.card {
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  overflow: hidden;
  background-color: var(--card-bg-color, #fff);
  height: 220px;
  width: 175px;
  transition: all 0.3s ease-in-out;
  cursor: pointer;
}

.card:hover {
  transform: translateY(-5px);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.2);
}

.card-img {
  width: 100%;
  height: 103px;
  object-fit: cover;
}

.card-body {
  padding: 0 15px;
  text-align: center;
  height: 100%;
}

.card-title {
  font-size: 16px;
  font-weight: 500;
  line-height: 1.2;
  margin: 0.5rem;
  color: var(--card-title-color, #333);
}

.card-text {
  font-size: 12px;
  font-weight: 400;
  color: var(--card-content-color, #6c757d);
}

.card-description {
  font-size: 0.7rem;
  margin: 5px 0;
  border-top: 1px solid #eee;
  padding-top: 5px;
  line-height: 1.5;
  text-align: justify;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  color:var(--card-text-color, #6c757d);
}
.no-data {
  text-align: center;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  align-items: center;
  justify-content: center;
  margin-top: 20px;
  font-size: 14px;
  color: #666;
}
@media (max-width: 992px) {
 .card-container {
    grid-template-columns: repeat(5, 1fr);
    gap: 10px;
  }
  }
@media (max-width: 768px) {
 .card-container {
    grid-template-columns: repeat(4, 1fr);
    gap: 5px;
  }
}
@media (max-width: 480px) {
 .card-container {
    grid-template-columns: repeat(2, 1fr);
  }
}
</style>
